package cn.jg.framework.web.controller;

import lombok.extern.slf4j.Slf4j;
import org.springframework.http.HttpStatus;
import org.springframework.web.context.request.RequestAttributes;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import java.net.URI;
import java.net.URISyntaxException;
import java.util.Objects;

/**
 * @author fanfever
 * @date 2016/7/9
 */
@Slf4j
public class BaseController {

	protected static final String APPLICATION_JSON = "application/json";

	protected String getRemoteHost() {
		RequestAttributes requestAttributes = RequestContextHolder.getRequestAttributes();
		if (Objects.isNull(requestAttributes)) {
			return null;
		}
		String url = ((ServletRequestAttributes) requestAttributes).getRequest()
				.getHeader("referer");
		URI uri;
		try {
			uri = new URI(url);
		} catch (URISyntaxException e) {
			log.warn("getRemoteHost url:{}, e:{}", url, e);
			return null;
		}
		return uri.getHost();
	}

	protected HttpStatus foundHttpStatus(Object object) {
//        boolean isHasContent;
//        if (object instanceof List) {
//            isHasContent = CollectionUtils.isNotEmpty((List) object);
//        } else {
//            isHasContent = Objects.nonNull(object);
//        }
//        return isHasContent ? HttpStatus.OK : HttpStatus.OK;
		return HttpStatus.OK;
	}

}
